//StudentMapper.java
package org.example.demo3.mapper;

import org.apache.ibatis.annotations.*;
import org.example.demo3.model.Student;

import java.util.List;

@Mapper  //定义下面的StudentMapper，创建一个Mybatis对象
public interface StudentMapper {
    //查询学生列表
    @Select("select * from student")
    List<Student> selectAll();

    //删除学生
    @Delete("delete from student where id=#{id}")
    int deleteById(int id);

    //添加学生
    @Insert("insert into student(name,password,sex,clazz,birthday) values(#{name},#{password},#{sex},#{clazz},#{birthday})")
    int insert(Student student);

    //查询对应id的学生
    @Select("select * from student where id=#{id}")
    Student selectById(int id);

    //更新学生
    @Update("update student set name=#{name},password=#{password},sex=#{sex},birthday=#{birthday},clazz=#{clazz} where id=#{id}")
    int update(Student student);

    //分页查询
    @Select("select * from student limit #{formIndex},#{pageSize}")
    @Results({
            @Result(column = "Id", property = "id", id = true),//id = true / javaType = int.class
            @Result(column = "Id",property = "scores",javaType = List.class,
                    many = @Many(select = "org.example.demo3.mapper.ScoreMapper.selectByStudentId"))
    })
    List<Student> selectByPage(int formIndex, int pageSize);

    //登录验证
    @Select("select * from student where name=#{username} and password=#{password}")
    Student selectByNameAndPwd(String username, String password);
}
